#include "lockimage.h"

LockImage::LockImage(QObject *parent)
    : QObject(parent)
{

}

LockImage::~LockImage()
{

}

bool LockImage::loadImage(const QString &path)
{
//    qDebug()<<"loadImage in " <<path<<this;
    if(mFirstLock.tryLock(0)) {
        bool load = mFirstImage.load(path);
        if(!load){
            qDebug()<<"loadImage out " <<path<<"error`"<<this;
        }
        mFirstLock.unlock();
//         qDebug()<<"loadImage out " <<path<<"0"<<this;
        return 0;
    } else if(mSecondLock.tryLock(0)){
        bool load = mSecondImage.load(path);
        if(!load){
            qDebug()<<"loadImage out " <<path<<"error2"<<this;
        }
        mSecondLock.unlock();
//         qDebug()<<"loadImage out " <<path<<"1"<<this;
        return 1;
    }
    qDebug()<<"loadImage out " <<path<<"-1"<<this;
    return -1;
}

void LockImage::stopReadLock(int index)
{
    if (index == 0) {
        mFirstLock.unlock();
    } else if (index == 1){
        mSecondLock.unlock();
    } else {

    }
}

int LockImage::startReadLock(QImage &img)
{
//    qDebug()<<"startReadLock in"<<this;
    if(mFirstLock.tryLock(0)) {
        img = mFirstImage;
//        qDebug()<<"startReadLock out 0"<<this;
        return 0;
    } else if(mSecondLock.tryLock(0)){
        img = mSecondImage;
//        qDebug()<<"startReadLock out 1"<<this;
        return 1;
    }
    qDebug()<<"startReadLock out -1"<<this;
    return -1;
}

